package cn.kgc.vue.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.kgc.vue.anno.LogAnnotation;
import cn.kgc.vue.common.Operation;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.entity.Permission;
import cn.kgc.vue.entity.RolePers;
import cn.kgc.vue.service.PermissionService;
import cn.kgc.vue.service.RolePersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author: 课工场
 * @since 2023/8/15
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePersService rolePersService;


    @RequestMapping("tree")
    public ResponseResult treeList() {
        List<Permission> list = permissionService.list();
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS, list);
        return responseResult;
    }

    // add  添加权限
    @PostMapping("add")
    @LogAnnotation(model = "权限模块",operation = Operation.ADD)
    @RequiresPermissions(value = {"sys:auth:add","sys:auth:update"},logical = Logical.OR)
    public ResponseResult addPermission(@RequestBody Permission permission) {

        permissionService.saveOrUpdate(permission);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        return responseResult;
    }

    //getById 根据id获取权限信息
    @GetMapping("getById")
    public ResponseResult getById(Integer id) {

        ResponseResult responseResult = new ResponseResult();
        // 根据id  查询当前权限的具体信息
        Permission permission = permissionService.getById(id);
        Integer pid = permission.getPid();  // 0

        HashMap<String, Object> map = new HashMap<>();

        if (pid == 0) {
            map.put("permission", permission);
            responseResult.success(ResponseStatus.SUCCESS, map);
            return responseResult;
        }

        // 根据权限的pid  查询父权限的title
        Permission parentPermission = permissionService.getById(pid);
        String title = parentPermission.getTitle();
        map.put("permission", permission);
        map.put("parentTitle", title);
        responseResult.success(ResponseStatus.SUCCESS, map);
        return responseResult;
    }

    //  根据id删除权限信息
    @DeleteMapping("deleteById")
    @RequiresPermissions("sys:auth:delete")
    @LogAnnotation(model = "权限模块",operation = Operation.DELETE)
    public ResponseResult deleteById(Integer id) {

        ResponseResult responseResult = new ResponseResult();
        //判定当前权限 是否含有子权限
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getPid, id);

        List<Permission> list = permissionService.list(lambda);
        if (ObjectUtil.isNotEmpty(list)) {
            responseResult.fail(ResponseStatus.HAS_CHILDNODE);
            return responseResult;
        }
        // 删除权限
        permissionService.removeById(id);
        //  角色权限关联表   删除  role_per
        LambdaQueryWrapper<RolePers> lambda1 = new QueryWrapper<RolePers>().lambda();
        lambda1.eq(RolePers::getPerId,id);
        rolePersService.remove(lambda1);
        responseResult.success();

        return responseResult;
    }

    @GetMapping("byName")
    public ResponseResult getByName(String name){
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getTitle,name);
        Permission one = permissionService.getOne(lambda);
        responseResult.success(ResponseStatus.SUCCESS,one);
        return responseResult;
    }
}
